package group6renderer;

import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.util.Arrays;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

/**
 * A class which creates the Graphical User Interface.
 * 
 * Collaborating Classes: Error log, Parser, Renderer
 * 
 * @author Joe Thomas
 * @version 0.3
 */
public class GUI
{
	private static GUI guiObj;
	private static boolean instance;
	
	/**
	 * Implementation of the singleton pattern
	 */
	public static GUI guiInstance()
	{
		if (!instance)
			{
				guiObj=new GUI();
				instance=true;
			}
		
		return guiObj;
	}
	
	
	
    // class variables (constants).
    private static final String[] SIZES =
    {
	"8", "10", "12", "14", "16", "20", "22"
    };
    private static final HashMap<String, Color> theColors = new HashMap<String, Color>();
    private static int INSTANCES = 0;
    // instance variables (fields).
    private Display display;
    private JComboBox sizeSelector;
    private JComboBox colorSelector;
    private JComboBox fontSelector;
    private JTextField searchbox;
    private JButton showErrors;
    private static JFrame window;
    private JFrame errorFrame;

    /**
     * A constructor to initialise the GUI's fields and to create each GUI element and and add them all together to form an aesthetic interface.
     * @param errors The errors dialog object to be shown when the errors button is pressed
     */
    private GUI()
    {
	
    	errorFrame=ErrorLog.errorInstance().getErrorsWindow();
	theColors.put("Black", Color.BLACK);
	theColors.put("Blue", Color.BLUE);
	theColors.put("Green", Color.GREEN);
	theColors.put("Pink", Color.MAGENTA);
	window = new JFrame("House 6's HTML Renderer");
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setSize(800, 850);
	Container content = window.getContentPane();
	content.setLayout(new BorderLayout());
	content.setBackground(Color.WHITE);
	display = new Display(Group6Renderer.treeRoot);
	JScrollPane sp = new JScrollPane(display);
	content.add(sp, BorderLayout.CENTER);
	searchbox = new JTextField();
	JPanel searchPanel=new JPanel();
	
	Font commandsFont=new Font(Display.DEFAULT_FONT,Font.ITALIC+Font.BOLD,14);
	searchbox.setFont(commandsFont);
	searchbox.setForeground(Color.RED);
	commandsFont=new Font(Display.DEFAULT_FONT,Font.BOLD,15);
	
	JLabel lsearch=new JLabel("Search text: ");
	lsearch.setFont(commandsFont);
	searchbox.setBackground(Color.CYAN);
	searchbox.addCaretListener(new CaretListener()
	{
	    public void caretUpdate(CaretEvent e)
	    {
		//This is to implement the search function, the display needs to be re-rendered
		display.search = searchbox.getText();
		display.paint(display.getGraphics());
	    }
	});
	
	searchPanel.setBackground(Color.CYAN);
	searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.X_AXIS));
	searchPanel.add(Box.createHorizontalGlue());
	searchPanel.add(lsearch);
	searchPanel.add(searchbox);
	searchPanel.add(Box.createHorizontalStrut(20));
	content.add(searchPanel, BorderLayout.PAGE_END);

	
	
	
	// commands container.
	JPanel commands = new JPanel();

	// text size selector
	sizeSelector = new JComboBox(SIZES);

	sizeSelector.setSelectedIndex(3);
	sizeSelector.setEditable(true);
	sizeSelector.addActionListener(new ActionListener()
	{
	/**
	 * This method handles the size dropdown selector behaviour
	 */
	    public void actionPerformed(ActionEvent e)
	    {
		
	    
	    	
	    	String g=(String)sizeSelector.getSelectedItem();
	    	
	    	if (g.isEmpty())
	    		return;

	    	for (int i=0;i<g.length();i++)
	    	{
	    		if (!Character.isDigit(g.charAt(i)))
	    			return;
	    	}
	    	
	    Display.TEXT_SIZE = Float.parseFloat((String) sizeSelector.getSelectedItem());
		display.repaint();
	    }
	});
	
	fontSelector = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
	fontSelector.addActionListener(new ActionListener()
	{
	    /**
	     * Method to handle the font selection drop down list
	     */
	    public void actionPerformed(ActionEvent e)
	    {
		Display.DEFAULT_FONT = (String) fontSelector.getSelectedItem();
		display.repaint();
	    }
	});
	fontSelector.setSelectedItem(Display.DEFAULT_FONT);
	// color selector
	Object[] colors = theColors.keySet().toArray();
	Arrays.sort(colors);
	colorSelector = new JComboBox(colors);
	
	colorSelector.addActionListener(new ActionListener()
	{
	    /**
	     * Method to handle the color selection dropdown
	     */
	    public void actionPerformed(ActionEvent e)
	    {
		
		Display.FOREGROUND_COLOR = theColors.get(colorSelector.getSelectedItem());
		display.repaint();
	    }
	});
	

	JLabel tSize=new JLabel("Text Size: ");tSize.setFont(commandsFont);
	commands.add(tSize);
	commands.add(sizeSelector);
	
	
	JLabel tColor=new JLabel("Text Color: ");tColor.setFont(commandsFont);
	commands.add(tColor);
	commands.add(colorSelector);

	
	
	
	
	JLabel tFont=new JLabel("Font: ");tFont.setFont(commandsFont);
	
	commands.add(tFont);
	commands.add(fontSelector);
	commands.setBackground(Color.CYAN);
	showErrors=new JButton("Show Errors");
	commandsFont=new Font(Display.DEFAULT_FONT,Font.BOLD,13);
	showErrors.setFont(commandsFont);
	showErrors.addActionListener(new ActionListener()
	{
	    //Method to show the error log window.
		public void actionPerformed(ActionEvent e)
		{
			errorFrame.setVisible(true);
		}
		
		
	});
	
	commands.add(showErrors);
	
	
	content.add(commands, BorderLayout.PAGE_START);
	window.setVisible(true);
	window.toFront();
    
    
    
    }
    
   
/*
 * Sets the title of the frame, used by the title tag
 * 
 * @param htmltitle The title string
 */
	public void setTitle(String htmlTitle)
	{
		window.setTitle(htmlTitle);
		
	}
}     